package com.yuan.hospital.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.bcel.internal.generic.IFNONNULL;
import com.yuan.hospital.repository.DepartmentRepository;
import com.yuan.hospital.service.DepartmentService;
import com.yuan.model.hospital.Department;
import com.yuan.vo.hosp.DepartmentQueryVo;
import com.yuan.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 科室信息
 *
 * @Author yuan
 * @Date 2021/7/14 13:57
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    /**
     * 上传科室
     *
     * @param map
     */
    @Override
    public void save(Map<String, Object> map) {
        //map转换成dept对象
        Department department = JSONObject.parseObject(JSONObject.toJSONString(map), Department.class);
        Department dept = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (dept != null) {
            dept.setUpdateTime(new Date());
            dept.setIsDeleted(0);
            departmentRepository.save(dept);
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.insert(department);
        }
    }

    /**
     * 查询科室
     *
     * @param page
     * @param limit
     * @param departmentQueryVo
     * @return
     */
    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        // 创建Pageable对象，设置当前页和每页记录数
        //0是第一页
        Pageable pageable = PageRequest.of(page-1,limit);
        // 创建Example对象
        Department department = new Department();
        //department.setHoscode(departmentQueryVo.getHoscode());
        BeanUtils.copyProperties(departmentQueryVo,department);
        Example<Department> example = Example.of(department);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    /**
     * 删除科室
     *
     * @param hosCode
     * @param depCode
     */
    @Override
    public void remove(String hosCode, String depCode) {
        Department dept = departmentRepository.getDepartmentByHoscodeAndDepcode(hosCode, depCode);
        if (dept != null) {
            departmentRepository.deleteById(dept.getId());
        }
    }

    /**
     * 根据医院编号，查询医院所有科室列表
     *
     * @param hosCode
     * @return
     */
    @Override
    public List<DepartmentVo> findDeptTree(String hosCode) {
        List<DepartmentVo> result = new ArrayList<>();
        //根据医院编号，查询医院所有科室信息
        Department deptQuery = new Department();
        deptQuery.setHoscode(hosCode);
        Example example = Example.of(deptQuery);
        //所有科室列表 departmentList
        List<Department> deptList = departmentRepository.findAll(example);
        //根据大科室编号  bigcode 分组，获取每个大科室里面下级子科室
        Map<String, List<Department>> deptMap = deptList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        for (Map.Entry<String, List<Department>> entry : deptMap.entrySet()) {
            //大科室编号
            String bigCode = entry.getKey();
            //大科室编号对应的全局数据
            List<Department> departmentList = entry.getValue();
            //封装大科室
            DepartmentVo deptVo = new DepartmentVo();
            deptVo.setDepcode(bigCode);
            deptVo.setDepname(departmentList.get(0).getBigname());
            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department dept : departmentList) {
                DepartmentVo departmentVo = new DepartmentVo();
                departmentVo.setDepcode(dept.getDepcode());
                departmentVo.setDepname(dept.getDepname());
                children.add(departmentVo);
            }
            deptVo.setChildren(children);
            result.add(deptVo);
        }
        return result;
    }

    /**
     * 根据科室编号，和医院编号，查询科室名称
     *
     * @param hosCode
     * @param depCode
     * @return
     */
    @Override
    public String getDepName(String hosCode, String depCode) {
        Department dept = departmentRepository.getDepartmentByHoscodeAndDepcode(hosCode, depCode);
        if (dept == null) {
            return dept.getDepname();
        }
        return null;
    }

    /**
     * 根据科室编号，和医院编号，查询科室
     *
     * @param hosCode
     * @param depCode
     * @return
     */
    @Override
    public Department getDepartment(String hosCode, String depCode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hosCode, depCode);
    }
}
